using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Hardware;

namespace MicroSnake
{
    class MenuScreen : Screen
    {
        public MenuScreen()
        {
            InitializeComponent();
        }

        private Text txtTitle;
        private ListBox menuList;
        private ListBoxTextMenuItem startGameMenuItem;
        private ListBoxTextMenuItem rankingMenuItem;
        private ListBoxTextMenuItem pauseMenuItem;
        private ListBoxTextMenuItem resumeMenuItem;
        private ListBoxTextMenuItem optionsMenuItem;
        private ListBoxTextMenuItem creditsMenuItem;
        
        /// <summary>
        /// Built the menu graphical user interface
        /// </summary>
        private void InitializeComponent()
        {
            menuList = new ListBox();
            menuList.HorizontalAlignment = HorizontalAlignment.Center;

            // Menu Title                
            txtTitle = new Text()
            {
                Font = Resources.GetFont(Resources.FontResources.NinaB),
                ForeColor = MicroSnakeColors.MenuTitleForeColor,
                HorizontalAlignment = HorizontalAlignment.Center,
                TextAlignment = TextAlignment.Center,
                TextContent = Resources.GetString(Resources.StringResources.Welcome),
                VerticalAlignment = VerticalAlignment.Center,
            };
            txtTitle.SetMargin(10);            

            menuList.Items.Add(new ListBoxMenuTitle(txtTitle));

            // Separator
            menuList.Items.Add(new ListBoxSpaceItem());

            // Menu Items
            startGameMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.StartGame);
            rankingMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.Ranking);
            pauseMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.Pause);
            resumeMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.Resume);
            optionsMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.Options);
            creditsMenuItem = ListBoxTextMenuItem.Make(Resources.StringResources.Credits);

            menuList.Items.Add(startGameMenuItem);
            menuList.Items.Add(rankingMenuItem);
            //menuList.Items.Add(PauseMenuItem);
            //menuList.Items.Add(ResumeMenuItem);
            menuList.Items.Add(optionsMenuItem);
            menuList.Items.Add(creditsMenuItem);

            menuList.SelectedItem = startGameMenuItem;

            // Add event handler
            menuList.SelectionChanged += new SelectionChangedEventHandler(menuList_SelectionChanged);
            menuList.AddHandler(Buttons.ButtonDownEvent, new ButtonEventHandler(menuList_ButtonDown), false);

            this.Children.Add(menuList);
        }

        /*********************************************************************/
        /*                       Event Handling                              */
        /*********************************************************************/

        void menuList_SelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            Debug.Print("Current selected item: " + menuList.Items[args.SelectedIndex].ToString() );
        }

        void menuList_ButtonDown(object sender, ButtonEventArgs e)
        {
            if (e.Button == Button.VK_SELECT)
            {
                if (menuList.SelectedItem == startGameMenuItem)
                {
                    ScreenManager.Instance.ShowGameScreen();
                }
                else if (menuList.SelectedItem == rankingMenuItem)
                {
                }
                else if (menuList.SelectedItem == pauseMenuItem)
                {
                }
                else if (menuList.SelectedItem == resumeMenuItem)
                {
                }
                else if (menuList.SelectedItem == optionsMenuItem)
                {
                    ScreenManager.Instance.ShowOptionsScreen();
                }
                else if (menuList.SelectedItem == creditsMenuItem)
                {
                    ScreenManager.Instance.ShowCreditsScreen();
                }
            }
        }

        /// <summary>
        /// Set the focus to the internal menuList
        /// </summary>
        public override void Focus()
        {
            Buttons.Focus(menuList);
        }
    }
}
